package com.zj.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zj.dao.AddressMapper;
import com.zj.dao.MyUserMapper;
import com.zj.dao.OrdersMapper;
import com.zj.model.Address;
import com.zj.model.MyUser;
import com.zj.model.Orders;
import com.zj.restApi.IdGenratorApi;
import com.zj.web.bean.OrderMessageDTO;
import com.zj.web.bean.OrdersDTO;
import com.zj.web.bean.ResponseResult;
import lombok.AllArgsConstructor;
import lombok.extern.java.Log;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Log
@AllArgsConstructor
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {
    private final AddressMapper addressMapper;
    private final OrdersMapper ordersMapper;
    private final MyUserMapper userMapper;
    private final IdGenratorApi idGenratorApi;
    //添加订单
    @Override
    @Transactional
    public OrdersDTO addOrder(OrdersDTO ordersDTO) {
        //先判断当前用户是否存在
        LambdaQueryWrapper<MyUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MyUser::getUserId, ordersDTO.getUserId());
        MyUser user = userMapper.selectOne(wrapper);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        //判断地址是否存在
        LambdaQueryWrapper<Address> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Address::getAddressId, ordersDTO.getAddressId());
        Address address = addressMapper.selectOne(wrapper1);
        if (address == null) {
            throw new RuntimeException("地址不存在");
        }
        //1.调用 idGenerator 服务 取出id号
        ResponseEntity<ResponseResult<Long>> responseEntity = idGenratorApi.getNextId();  //jdk动态代理对象
        if(responseEntity.getStatusCode()!= HttpStatus.OK || responseEntity.getBody()==null){
            throw new RuntimeException("调用idGenerator服务失败");
        }
        ResponseResult<Long> responseResult = responseEntity.getBody();
        log.info(responseResult.toString());
        if (responseResult == null || responseResult.getCode() != 1) {
            throw new RuntimeException("调用idGenerator服务失败");
        }
        Long id = responseResult.getData();
        ordersDTO.setOrderId(id);
        ordersDTO.setOrderStatus(ORDER_STATUS_UNPAID);
        ordersDTO.setOrderTime(new Date());

        Orders orders=new Orders();
        BeanUtils.copyProperties(ordersDTO,orders);
        ordersMapper.insert(orders);
        return ordersDTO;
    }

    //根据订单状态和用户Id查询订单详情信息
    @Override
    public List<OrdersDTO> findUnfinishedOrder(Integer order_status1,Long user_id) {
        // 直接传递参数，由MyBatis动态SQL处理
        return ordersMapper.selectByOrderStatusWithProduct(order_status1, user_id);
    }

    //根据订单id修改订单状态
    @Override
    @Transactional
    public boolean updateOrderStatus(Long orderId, Integer orderStatus,String email) {
        Orders orders = ordersMapper.selectById(orderId);
        if (orders == null) {
            throw new RuntimeException("订单不存在");
        }
        orders.setOrderStatus(orderStatus);
        return ordersMapper.updateById(orders) > 0;
    }

    //根据订单id查询订单详情信息
    @Override
    @Transactional
    public List<OrdersDTO> findOrderById(Long orderId) {
        return ordersMapper.selectByOrderIdWithProduct(orderId);
    }

    @Override
    public OrderMessageDTO addOrderByAI(OrdersDTO ordersDTO) {
        //先判断当前用户是否存在
        LambdaQueryWrapper<MyUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MyUser::getUserId, ordersDTO.getUserId());
        MyUser user = userMapper.selectOne(wrapper);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        //判断地址是否存在
        LambdaQueryWrapper<Address> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Address::getAddressId, ordersDTO.getAddressId());
        Address address = addressMapper.selectOne(wrapper1);
        if (address == null) {
            throw new RuntimeException("地址不存在");
        }
        //1.调用 idGenerator 服务 取出id号
        ResponseEntity<ResponseResult<Long>> responseEntity = idGenratorApi.getNextId();  //jdk动态代理对象
        if(responseEntity.getStatusCode()!= HttpStatus.OK || responseEntity.getBody()==null){
            throw new RuntimeException("调用idGenerator服务失败");
        }
        ResponseResult<Long> responseResult = responseEntity.getBody();
        log.info(responseResult.toString());
        if (responseResult == null || responseResult.getCode() != 1) {
            throw new RuntimeException("调用idGenerator服务失败");
        }
        Long id = responseResult.getData();
        ordersDTO.setOrderId(id);
        ordersDTO.setOrderStatus(ORDER_STATUS_UNPAID);
        ordersDTO.setOrderTime(new Date());

        Orders orders=new Orders();
        BeanUtils.copyProperties(ordersDTO,orders);
        ordersMapper.insert(orders);
        OrderMessageDTO orderMessageDTO=new OrderMessageDTO();
        BeanUtils.copyProperties(ordersDTO,orderMessageDTO);
        return orderMessageDTO;
    }

    @Override
    public Integer findOrderStatusById(Long orderId) {
        Orders orders = ordersMapper.selectById(orderId);
        if (orders == null) {
            return null;
        }
        return orders.getOrderStatus();
    }


}
